#!/bin/bash
source /etc/profile
readonly PLATFORMS=("linux" "windows")
readonly ARCH=amd64
readonly APP_ID="com.klyx.video2m3u8"
readonly ICON_PATH="./resources/Icon.png"
readonly GOPROXY="https://goproxy.io,direct"
readonly DIST_DIR="dist"
readonly ZIP_FILE="dist_${1}.zip"


# 定义一个函数，用于编译、移动文件和打包
compile_and_package() {
    local platform=$1
    # 参数判断
    if ! [[ " ${PLATFORMS[@]} " =~ " ${platform} "  ]]; then
        echo "请输入参数 linux 或 win"
        exit 1
    fi
    # 初始化：删除旧的文件和目录，创建新的目录
    rm -rf "$DIST_DIR" "$DIST_DIR.zip" && mkdir -p "$DIST_DIR/bin"
    fyne-cross "$platform" -arch="$ARCH" -app-id="$APP_ID" -icon="$ICON_PATH" -env="GOPROXY=$GOPROXY"
    if [[ $? -ne 0 ]]; then
        echo "编译 $platform 失败"
        exit 1
    fi

    local binary_name
    if [[ "$platform" == "linux" ]]; then
        binary_name="video2m3u8"
    else
        binary_name="video2m3u8.exe"
    fi

    mv "fyne-cross/bin/$platform-$ARCH/$binary_name" "$DIST_DIR/"
    cp "./bin/ffmpeg$(test "$platform" = 'linux' && echo || echo '.exe')" "$DIST_DIR/bin/ffmpeg$(test "$platform" = 'linux' && echo || echo '.exe')"
    zip -r "$ZIP_FILE" "$DIST_DIR"
    echo "打包成功"
}

# 执行
compile_and_package "$1"

